/*
 * Copyright (c) 2024 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "nm_ipc_client.h"

#include "networkvpn_client.h"
#include "nm_log.h"
#include "nm_type.h"
#include "securec.h"

using namespace std;
using namespace OHOS::NetManagerStandard;

int32_t GetVpnCertUri(enum vpn_cert_type type, uint8_t *certUrl, uint32_t buffSize)
{
    std::string uri;
    int32_t ret = NetworkVpnClient::GetInstance().GetSysVpnCertUri(type, uri);
    if (ret != 0) {
        NET_LOG_E("GetVpnCertUri failed. ret:%{public}d type:%{public}d", ret, type);
        return 0;
    }

    uint32_t size = strlen(uri.c_str());
    size = size < buffSize ? size : buffSize;
    NET_LOG_I("GetVpnCertUri type:%{public}d size:%{public}d", type, size);

    (void)memset_s(certUrl, buffSize, 0, buffSize);
    memcpy_s(certUrl, buffSize, (uint8_t *)(uri.c_str()), size);

    return size;
}

void SetupVpnIfconfig(const char *ifconfig, const char *netmask, int mtu, DNSList *dns_list)
{
    char config[BUFSIZ] = {0};
    sprintf(config,
        "openvpn{\"config\":{"
        "\"address\":\"%s\","
        "\"netmask\":\"%s\","
        "\"mtu\":%d,"
        "\"primarydns\":\"%s\","
        "\"secondarydns\":\"%s\""
        "}}\n",
        ifconfig, netmask, mtu, dns_list->dns_servers[0], dns_list->dns_servers[1]);

    int32_t ret = NetworkVpnClient::GetInstance().NotifyConnectStage(std::string(config), STATE_SUCESS);
    if (ret != 0) {
        NET_LOG_E("SetupVpnIfconfig failed");
    } else {
        NET_LOG_I("SetupVpnIfconfig success");
    }
}

void NotifyVpnState(enum vpn_state state)
{
    char config[BUFSIZ] = {0};
    sprintf(config, "openvpn{\"updateState\":{\"state\":%d}}\n", state);
    int32_t ret = NetworkVpnClient::GetInstance().NotifyConnectStage(std::string(config), STATE_SUCESS);
    if (ret != 0) {
        NET_LOG_E("NotifyVpnState failed: ret=%{public}d state=%{public}d", ret, state);
    } else {
        NET_LOG_I("NotifyVpnState success state=%{public}d", state);
    }
}

int32_t SetUpVpnTun()
{
    char config[BUFSIZ] = {0};
    sprintf(config, "openvpn{\"setupVpnTun\":{\"state\":%d}}\n", 0);
    int32_t ret = NetworkVpnClient::GetInstance().NotifyConnectStage(std::string(config), STATE_SUCESS);
    if (ret != 0) {
        NET_LOG_E("NotifyVpnState failed: ret=%{public}d state=%{public}d", ret, 0);
    } else {
        NET_LOG_I("NotifyVpnState success state=%{public}d", 0);
    }
    return ret;
    // return NetworkVpnClient::GetInstance().SetUpVpnTun();
}